--[[
从优先级队列中读取短信消息并通过平台ID进行过滤KEYS[1]中读取[序列:号码],并读取其关联[KEYS[2]:序号:号码]HASH中的内容,返回指定长度的List

KEYS[1] : 优先级队列标识
KEYS[2] : hash key前缀, 和KEYS[1]读取的字符串组成完整的hash key
ARGV[1] : 返回的最大list长度

--]]


local prioritylist = KEYS[1]
local hashprefix = KEYS[2]
local count = tonumber(ARGV[1])
local serid = ARGV[2]





local function main(prioritylist,hashprefix,count,serid)

local array = {}
local index = 0

--[[--
	local keylist = redis.call("scan",0,"MATCH",prioritylist..":".."*","COUNT",count)
	local keylist = redis.call("keys",prioritylist..":".."*")
--]]

local keylist = redis.call("SMEMBERS",prioritylist)
for i,v in ipairs(keylist) do
	if(v ~= nil and v ~= "" and v ~= false) then
		if(v ~= nil or v ~= "" and v ~= false  and type(v) == "string" ) then
			local sizestr = redis.call("llen",v)
			if(sizestr ~= nil and sizestr ~= "" and sizestr ~= false) then
				local size = tonumber(sizestr)
				local smlist = redis.call("lrange",v,0,size)	
				if(smlist ~= nil and smlist ~= "" and smlist ~= false) then
					for j,m in ipairs(smlist) do
						if(m ~= nil or m ~= "" ) then
							local modelname = redis.call("hmget",hashprefix..":"..m,"RecvModuleName") 
							local name = modelname[1]
							if(name == serid) then
							local hashvalue = redis.call("hmget", hashprefix..":"..m,"Srv_node","OrgSerialNo","SendPort","User","Smcont","SmType","RecvModuleName" )
							index = index +1
							if( index > count) then
								break
							end
							array[index] = hashvalue
							redis.call("lrem",v,j,m)
							redis.call("del",hashprefix..":"..m)
							end
						end
					end
				end
			end
		end
	end
end
return array
end


 return main(prioritylist,hashprefix,count,serid);
